<?php

/* 
 * 全局用的方法
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class common
{ 
    public static function load(){
        return new self();
    }
    /**
     * 加载X配置文件
     * @return type
     */
    public function load_x_config() {        
        $data = read_static_cache('x_config');
        if($data == false) {
            $data = configModel::load()->get_list();
            write_static_cache('x_config', $data);
        }
        return $data;
    }
    

    /**
     * 加载指定文件夹所有的类
     * @param string $dir 目录夹子
     * @param array $exdir 排除的文件列表 eg:array('text.php');
     */
    public static function load_dir($dir, $exdir = array()) {
        $files = self::_getFiles($dir, $exdir);
        if($files) {
            foreach ($files as $file) {
                require($file);     
            }           
        }
    }

    /**
     * 加载指定类文件
     * @param string $dir 目录名称
     * @param string $class_name 文件名,不需要.php
     * @param array $exdir 排除文件夹
     */
    public static function load_class($dir, $class_name, $exdir = array()) {
        if(is_dir($dir)) {
            if(strpos($class_name, '.') === false) {
                $file_path = $dir . $class_name . '.php';
            } else {
                $file_path = $dir . $class_name;
            }
            $file_path = self::_replace($file_path);
            if(file_exists($file_path)) {
                require($file_path);
            } else {
                mylogger($file_path . '不存在', 'load_class');
            }
        } else {
            mylogger($dir . '不存在', 'load_class');
        }        
    }
    /**
     * 获取配置文件目录
     * @param type $class_name
     * @return string 返回
     */
    public static function get_config_file($class_name) {
        $file = CONFIG_PATH . $class_name;
        $file_path = str_replace('.php', '', $file) . '.php';
        if(file_exists($file_path)) {
            return $file_path;
        } else {
            return false;            
        }        
    }

    /**
     * 获取指定文件夹里的文件所有列表
     * @param type $path
     * @param type $exdir
     * @return boolean|array
     */
    private static function _getFiles($path, $exdir) {
        $files = scandir($path);
        if(count($files) > 2 && is_dir($path)) {
            unset($files[0], $files[1]);
            $new_files = array();
            foreach ($files as $file) {
                if(strpos($file, '.php') > 0) {
                    if(!in_array($file, $exdir)) {                        
                        $new_files[] = $path . DIRECTORY_SEPARATOR . $file;
                    }
                }
            }
            return $new_files;
        }
        return false;
    }
    private static function _replace($url) {
        return str_replace('\\', '/', $url);
    }
    
    /**
     * 获取12位长度的APPid
     * @return type
     */
    public static function get_appid(){
        $api = new ApiBase();
        $nonce = $api->getNonceStr();
        $uid = uniqid();
        $md5_str = md5($nonce . md5($uid));
        $appId = substr($md5_str, 0, 12);        
        return strtoupper($appId);
    }
    /**
     * 获取32位长度的APPKEY
     * @return type
     */
    public static function get_appkey(){
        $api = new ApiBase();
        $nonce = $api->getNonceStr();
        $uid = uniqid();
        $md5_str = md5($nonce . md5($uid));
        $appId = substr($md5_str, 0, 32);        
        return strtoupper($appId);
    }
}
